package netbayServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import netbay.NetbayUtility;
import netbay.TransactionManager;
import netbay.UserManager;
import util.String.StringUtil;

/**
 * Servlet implementation class AddGroupServlet
 */
public class AddGroupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddGroupServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init() {
		String configPath = getServletContext().getRealPath(
				"/WEB-INF/config.properties");
		NetbayUtility.init(configPath);
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
        response.setContentType("text/json;charset=utf-8");
        HttpSession session = request.getSession();
        String url = "";
        String userId = ((!StringUtil.isNullOrEmpty((String)(session.getAttribute("userId")))) ? (String)session.getAttribute("userId"): "null");
        if(!(userId.equals("null"))){
        	url = "/AddGroup.jsp";
        }
        else{
        	url = "/Login.jsp";
        }
        try{
        request.getRequestDispatcher(url).forward(request, response);
			} catch (Exception ex) {
	            ex.printStackTrace();
	        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
	        String userPath = request.getServletPath();
	        HttpSession session = request.getSession();
	        response.setContentType("application/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        String strJson = "{}";
	        UserManager um = new UserManager();
	        
	        if (userPath.equals("/AddGroupServlet/CheckGroup")) {
	        	String group = (!StringUtil.isNullOrEmpty(request.getParameter("group"))) ? request.getParameter("group").trim() : "";       	
	        	boolean result = false;
	   		 	result = um.checkGroup(group);
	   		 	if(result){
	   		 		strJson = "{\"success\": \"\"}";
	   		 	}else{
	   		 	strJson = "{\"error\": \"Cannot check name of group.\"}";
	   		 	}
	   		 	out.println(strJson);
	        }else if (userPath.equals("/AddGroupServlet/AddNewGroup")) {
	        	try {
		        	String group = (!StringUtil.isNullOrEmpty(request.getParameter("group"))) ? request.getParameter("group").trim() : "";
			        //Integer tcdTracking = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("tcdTracking"))) ? "1" : "0");
		        	Integer tcdTracking = 1;
		        	Integer rdTracking = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("rdTracking"))) ? "1" : "0");
			        Integer tcdBilling = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("tcdBilling"))) ? "1" : "0");
			        Integer rdBilling = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("rdBilling"))) ? "1" : "0");
			        Integer manageUsers = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("manageUsers"))) ? "1" : "0");
			        Integer manageGroups = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("manageGroups"))) ? "1" : "0");
			        Integer userLog = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("userLog"))) ? "1" : "0");
			        Integer reTCDTCD = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("reTCDTCD"))) ? "1" : "0");
			        Integer reJPMCTCD = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("reJPMCTCD"))) ?"1" : "0");
			        Integer reRDRD = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("reRDRD"))) ? "1"  : "0");
			        Integer reJPMCRD = Integer.parseInt((!StringUtil.isNullOrEmpty(request.getParameter("reJPMCRD"))) ? "1" : "0");
	
		        	boolean result = false;
		   		 	result = um.insertNewGroup(group,tcdTracking,rdTracking,tcdBilling,rdBilling,manageUsers,manageGroups,userLog,reTCDTCD,reJPMCTCD,reRDRD,reJPMCRD);
		   		 	
		   		 	if(result){
						um.saveLog((String)session.getAttribute("userId"),UserManager.actionAddNewGroup,group);
						strJson = "{\"success\": \"\"}";
		   		 	}else{
		   		 		strJson = "{\"error\": \"Cannot add new group.\"}";
		   		 	}
	   		 	} catch (Exception ex) {
	   		 		strJson = "{\"error\": \"" + ex.getMessage() + "\"}" ;
				}
	   		 	out.println(strJson);
	        }
	}

}
